import 'package:flutter/material.dart';
import 'package:schoollifedemo/business/personal/viewmodel/personal_home_page_viewmodel.dart';
import 'package:schoollifedemo/commonwidget/circle_empty_widget.dart';
import 'package:schoollifedemo/commonwidget/common_base_page.dart';
import 'package:schoollifedemo/commonwidget/common_dialog.dart';
import 'package:schoollifedemo/commonwidget/error_widget.dart';
import 'package:schoollifedemo/commonwidget/loading_widget.dart';
import 'package:schoollifedemo/commonwidget/text_widget_with_overflow.dart';
import 'package:schoollifedemo/entity/Entity.dart';

class PersonalPage extends BaseStatefulPage {
  const PersonalPage({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _PersonalPageState();
  }
}

class _PersonalPageState extends State<PersonalPage> {
  final PersonalHomePageViewModel _viewModel = PersonalHomePageViewModel();

  @override
  void initState() {
    _viewModel.init();
    _viewModel.pullPersonalInfo(
      onFailure: (msg) {
        DialogUtils.showTitleAndMessageDialog(
          context,
          clickOutSideDismiss: true,
          title: '错误',
          msg: msg,
        );
      },
    );
    super.initState();
  }

  @override
  void dispose() {
    _viewModel.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: StreamBuilder(
        initialData: CommonLoadingState.loading,
        stream: _viewModel.loadingState,
        builder: (context, snapshot) {
          CommonLoadingState? loadingState =
              snapshot.data as CommonLoadingState?;
          switch (loadingState) {
            case CommonLoadingState.loading:
              return _loadingWidget();
            case CommonLoadingState.successTypeOne:
            case CommonLoadingState.successTypeTwo:
              return _personalBody();
            default:
              return CommonErrorWidget(
                '错误',
                onButtonClick: () {
                  _viewModel.pullPersonalInfo(
                    onFailure: (msg) {
                      DialogUtils.showTitleAndMessageDialog(
                        context,
                        clickOutSideDismiss: true,
                        title: '错误',
                        msg: msg,
                      );
                    },
                  );
                },
              );
          }
        },
      ),
    );
  }

  Widget _loadingWidget() {
    return Container(
      color: Colors.white,
      child: const LoadingWidget(),
    );
  }

  Widget _personalBody() {
    return Container(
      color: Colors.white,
      padding: const EdgeInsets.only(bottom: 10),
      child: RefreshIndicator(
        onRefresh: () async {
          _viewModel.pullPersonalInfo(
            onFailure: (msg) {
              DialogUtils.showTitleAndMessageDialog(
                context,
                clickOutSideDismiss: true,
                title: '错误',
                msg: msg,
              );
            },
          );
        },
        child: ListView(
          physics: const BouncingScrollPhysics(),
          children: [
            const TextWidgetWithOverflow(
              '你的信息',
              textAlign: TextAlign.left,
              insideColor: Colors.transparent,
              paddingLeft: 25,
              fontSize: 28,
            ),
            Padding(
              padding: const EdgeInsets.only(
                  left: 20, right: 20, bottom: 10, top: 10),
              child: CircleEmptyWidget(
                Container(
                  margin:
                      const EdgeInsets.only(left: 10, right: 10, bottom: 10),
                  child: _personalWidget(),
                ),
                insideColor: const Color(0x66D0E7FF),
              ),
            ),
            const TextWidgetWithOverflow(
              '应用设置',
              textAlign: TextAlign.left,
              insideColor: Colors.transparent,
              paddingLeft: 25,
            ),
            Padding(
              padding: const EdgeInsets.only(left: 20, right: 20, top: 10),
              child: CircleEmptyWidget(
                _appSettingWidget(),
                insideColor: const Color(0x66D0E7FF),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _personalWidget() {
    return Column(
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            GestureDetector(
              onTap: () async {
                // 右上方更改状态
                await _viewModel.turnToEditPersonalInfoPage(context);
                _viewModel.pullPersonalInfo(
                  onFailure: (msg) {
                    DialogUtils.showTitleAndMessageDialog(
                      context,
                      clickOutSideDismiss: true,
                      title: '错误',
                      msg: msg,
                    );
                  },
                );
              },
              child: Container(
                margin: const EdgeInsets.only(top: 10, right: 5),
                width: 24,
                height: 24,
                child: Image.asset('assets/images/common/icon_edit.png'),
              ),
            ),
          ],
        ),
        Container(
          width: 80,
          height: 80,
          margin: const EdgeInsets.only(top: 10),
          child: ClipOval(
            child: Image.network(
              _viewModel.getIconAddress(),
              fit: BoxFit.cover,
            ),
          ),
        ),
        TextWidgetWithOverflow(
          _viewModel.getPersonalInfoResponse()?.nickname,
          insideColor: Colors.transparent,
          fontSize: 18,
          textColor: const Color(0xFF429CFF),
          fontWeight: FontWeight.w500,
        ),
        Padding(
          padding: const EdgeInsets.only(left: 5, right: 5, top: 10),
          child: CircleEmptyWidget(
            _personalSelfIntroduce(),
          ),
        ),
        Padding(
          padding: const EdgeInsets.only(top: 20, left: 5, right: 5),
          child: Row(
            children: [
              Expanded(
                child: GestureDetector(
                  onTap: () {
                    _viewModel.turnToPersonalFriendCirclePage(context);
                  },
                  child: CircleEmptyWidget(
                    Row(
                      children: [
                        const TextWidgetWithOverflow(
                          '朋友圈',
                          fontSize: 14,
                          insideColor: Colors.transparent,
                          width: 80,
                        ),
                        Expanded(
                          child: CircleEmptyWidget(
                            TextWidgetWithOverflow(
                              _viewModel
                                  .getPersonalInfoResponse()
                                  ?.friendCircleCount
                                  ?.toString(),
                              fontSize: 14,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
              const SizedBox(width: 10),
              Expanded(
                child: CircleEmptyWidget(
                  Row(
                    children: [
                      const TextWidgetWithOverflow(
                        '性别',
                        fontSize: 14,
                        insideColor: Colors.transparent,
                        width: 80,
                      ),
                      Expanded(
                        child: CircleEmptyWidget(
                          TextWidgetWithOverflow(
                            _viewModel.getUserSex(),
                            fontSize: 14,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
        Padding(
          padding: const EdgeInsets.only(left: 5, right: 5, top: 10, bottom: 5),
          child: CircleEmptyWidget(
            Row(
              children: [
                const TextWidgetWithOverflow(
                  '我的学校',
                  fontSize: 14,
                  insideColor: Colors.transparent,
                  width: 80,
                ),
                Expanded(
                  child: CircleEmptyWidget(
                    TextWidgetWithOverflow(
                      _viewModel.getPersonalInfoResponse()?.school?.schoolName,
                      fontSize: 14,
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
        Padding(
          padding: const EdgeInsets.only(left: 5, right: 5, top: 5, bottom: 5),
          child: CircleEmptyWidget(
            Row(
              children: [
                const TextWidgetWithOverflow(
                  '手机号码',
                  fontSize: 14,
                  insideColor: Colors.transparent,
                  width: 80,
                ),
                Expanded(
                  child: CircleEmptyWidget(
                    TextWidgetWithOverflow(
                      _viewModel.getPersonalInfoResponse()?.phone,
                      fontSize: 14,
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
        Padding(
          padding: const EdgeInsets.only(left: 5, right: 5, top: 5, bottom: 5),
          child: CircleEmptyWidget(
            Row(
              children: [
                const TextWidgetWithOverflow(
                  '地区',
                  fontSize: 14,
                  insideColor: Colors.transparent,
                  width: 80,
                ),
                Expanded(
                  child: CircleEmptyWidget(
                    TextWidgetWithOverflow(
                      _viewModel.getPersonalInfoResponse()?.area,
                      fontSize: 14,
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
        Padding(
          padding: const EdgeInsets.only(left: 5, right: 5, top: 5, bottom: 5),
          child: CircleEmptyWidget(
            Row(
              children: [
                const TextWidgetWithOverflow(
                  '学号',
                  fontSize: 14,
                  insideColor: Colors.transparent,
                  width: 80,
                ),
                Expanded(
                  child: CircleEmptyWidget(
                    TextWidgetWithOverflow(
                      _viewModel.getPersonalInfoResponse()?.studentId,
                      fontSize: 14,
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
        Padding(
          padding: const EdgeInsets.only(left: 5, right: 5, top: 5, bottom: 5),
          child: CircleEmptyWidget(
            Row(
              children: [
                const TextWidgetWithOverflow(
                  '我的账号',
                  fontSize: 14,
                  insideColor: Colors.transparent,
                  width: 80,
                ),
                Expanded(
                  child: CircleEmptyWidget(
                    TextWidgetWithOverflow(
                      _viewModel.getPersonalInfoResponse()?.account,
                      fontSize: 14,
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
        Offstage(
          offstage: true,
          child: Padding(
            padding:
                const EdgeInsets.only(left: 5, right: 5, top: 5, bottom: 5),
            child: CircleEmptyWidget(
              Row(
                children: [
                  const TextWidgetWithOverflow(
                    '教师号',
                    fontSize: 14,
                    insideColor: Colors.transparent,
                    width: 80,
                  ),
                  Expanded(
                    child: CircleEmptyWidget(
                      TextWidgetWithOverflow(
                        _viewModel.getPersonalInfoResponse()?.teacherId,
                        fontSize: 14,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ],
    );
  }

  Widget _personalSelfIntroduce() {
    return Column(
      children: [
        const Padding(
          padding: EdgeInsets.only(top: 10, left: 10, right: 10),
          child: TextWidgetWithOverflow(
            '我的简介:',
            insideColor: Colors.transparent,
            fontSize: 18,
            width: double.infinity,
            textAlign: TextAlign.left,
          ),
        ),
        Padding(
          padding: const EdgeInsets.only(left: 10, right: 10, bottom: 10),
          child: TextWidgetWithOverflow(
            _viewModel.getPersonalInfoResponse()?.personalProfile,
            fontSize: 16,
            textAlign: TextAlign.left,
            maxLine: 50,
            width: double.infinity,
            insideColor: Colors.transparent,
          ),
        ),
      ],
    );
  }

  Widget _appSettingWidget() {
    return Column(
      children: [
        GestureDetector(
          onTap: () {
            _viewModel.turnToPasswordChangePage(context);
          },
          child: const Padding(
            padding: EdgeInsets.only(top: 15, bottom: 5, left: 10, right: 10),
            child: TextWidgetWithOverflow(
              '密码修改',
              fontSize: 14,
              width: double.infinity,
              height: 40,
            ),
          ),
        ),
        GestureDetector(
          onTap: () {},
          child: const Padding(
            padding: EdgeInsets.only(top: 5, bottom: 5, left: 10, right: 10),
            child: TextWidgetWithOverflow(
              '教师认证',
              fontSize: 14,
              width: double.infinity,
              height: 40,
            ),
          ),
        ),
        GestureDetector(
          onTap: () {
            _viewModel.turnToPrivacyPage(context);
          },
          child: const Padding(
            padding: EdgeInsets.only(top: 5, bottom: 5, left: 10, right: 10),
            child: TextWidgetWithOverflow(
              '隐私',
              fontSize: 14,
              width: double.infinity,
              height: 40,
            ),
          ),
        ),
        GestureDetector(
          onTap: () {
            _viewModel.turnToSettingsPage(context);
          },
          child: const Padding(
            padding: EdgeInsets.only(top: 5, bottom: 15, left: 10, right: 10),
            child: TextWidgetWithOverflow(
              '软件设置',
              fontSize: 14,
              height: 40,
              width: double.infinity,
            ),
          ),
        ),
      ],
    );
  }
}
